---
title: "ISQ Replication Figures and Summary Statistics"
date: "2024-11-14"
output:
  pdf_document: default
  html_document:
    df_print: paged
---

Replication file for the article "Exit Clauses in International Investment Agreements" by Tuuli-Anna Huikuri and Sujeong Shim

```{r setup, include=FALSE}
options(repos = c(CRAN = "https://cran.r-project.org"))
knitr::opts_chunk$set(echo = TRUE)
```

```{r}
install.packages("pacman")
pacman::p_load(haven, foreign, tidyverse, ggplot2, ggthemes, interplot, stargazer)
```

```{r}
iias <- read_dta("~/Huikuri_Shim_maindataset2025.dta")
meltframe1 <- read.csv("~/meltframe1.csv")
bleu <- read.csv("~/Deviation from BLEU model BITs.csv")
```

Figure 1. Variation in the flexibility of exit clauses in BITs. 1959-2018
```{r}
# Reorder levels
meltframe1$variable <- factor(meltframe1$variable, 
                              levels = c("anytimeafter_clauses", "termwindow_clauses", "anytime_clauses"))

fig1 <- ggplot(data = meltframe1, aes(x = year, y = value, group = variable,
                                      shape = variable, fill = variable)) +
  geom_density(data = subset(meltframe1, variable %in% c("anytimeafter_clauses", "termwindow_clauses", "anytime_clauses")),
               aes(x = year, y = value, fill = variable),
               stat = "identity",
               alpha = 0.8, color = "black") +
  theme_clean() +
  theme(plot.title = element_text(hjust = 0.5, size = 18),
        axis.title = element_text(size = 16)) +
  theme(strip.text.x = element_blank()) +
  scale_fill_manual(values = c("brown1", "tomato4", "navajowhite2"),
                    labels = c("Anytime after initial", "Termination window", "Anytime"),
                    name = "Exit clause") +
  ggtitle("Exit clauses in BITs, 1959-2018") +
  labs(y = "Number of BITs signed", x = "Year")
fig1
```

Figure 2. Deviations of effective commitment period from the BLEU model BIT
```{r}
fig2 <- ggplot(bleu, aes(x=deviation)) +
  geom_bar(width = 0.5, fill = "lightgrey", color = "black", position = "dodge2") +
  geom_text(stat = "count", aes(label = stat(count), y = ..count..), vjust = -0.4, size = 4) + 
  labs(title = "Deviations of effective commitment period from BLEU model BIT", x = "Commitment period deviations from Model BIT (years)", y = "No. of BITs") +
  scale_y_continuous("Count of BITs") +
  scale_x_continuous(breaks = seq(-10, 31, 5)) +
  theme_clean() +
  theme(plot.title = element_text(hjust = 0.5))
fig2
```
Figure 3. Effective commitment period in BITs
```{r}
effective <- iias$effective_commitment_years
fig3 <- ggplot(data=NULL, aes(x=effective)) +
  geom_histogram(alpha=0.8, position="identity", color = "black", fill = "grey") +
  labs(x="Effective commitment years", y = "Frequency",
       title = "Effective commitment years in BITs") +
  theme_clean() +
  theme(plot.title = element_text(hjust = 0.5))
fig3
```

Figure 4. Marginal effect of importer's PR protection on effective commitment period
```{r}
table3_model3 <- lm(effective_commitment_years ~ dem_accountability1 + dem_accountability1*prights_vdem2 + + fdi_inflows_ofgdp1 + fdi_inflows_ofgdp2 + tradeof_gdp1 + tradeof_gdp2 + isds_respondent1_cum + isds_respondent2_cum + bur_quality1 + bur_quality2 + gov_stability1 + gov_stability2 + yo_signature + auto_renewal_num + ex_security_num + ex_healthenv_num + ex_other_num + ex_prudential_num + term_window + imr, data = iias)

fig4 <- interplot(m = table3_model3, var1 = "prights_vdem2", var2 = "dem_accountability1", hist = T) +
  labs(title = "Marginal effect of importers's property rights on commitment period", x = "Exporters's democratic accountability", y = "Effect of 1 s.d. increase in importer's property rights") +
  theme_clean() +
  theme(plot.title = element_text(hjust = 0.5))
fig4
```

Figure 5. Relationship between substantive flexibility and exit flexibility
```{r}
fig5 <- ggplot(data = iias, aes(x = effective_commitment_years, y = total_srssubs_01)) +
  geom_point() +
  labs(x = "Effective commitment period",
       y = "SRS substantive",
       title = "Substantive flexibility and effective commtiment period in BITs") +
  theme_clean() +
  theme(plot.title = element_text(hjust = 0.5))
fig5
```

Table A1. Summary statistics
```{r}
vars <- iias %>% 
  subset(select = c("term_flexibility_num",
                      "anytime",
                      "anytime_after",
                      "term_window",
                      "effective_commitment_years",
                    
                      "dem_accountability1",
                      "prights_vdem2",
                    
                      "yo_signature",
                      "initial_term_num",
                      "auto_renewal_num",
                      "uni_term_num",
                      "amendment_num",
                      "sunset_num",
                      "ex_healthenv_num",
                      "ex_security_num" ,
                      "ex_other_num" ,
                      "ex_prudential_num" ,
                      "isds_num" ,
                      "ssds_num" ,
                      "total_srssubs_01",
                    
                      "fdi_inflows_ofgdp1",
                      "fdi_inflows_ofgdp2",
                      "tradeof_gdp1",
                      "tradeof_gdp2",
                      "isds_respondent1_cum",
                      "isds_respondent2_cum",
                      "gov_stability1",
                      "gov_stability2",
                      "fh_ipolity2_1",
                      "fh_ipolity2_2",
                      "polity1",
                      "polity2",
                      "bur_quality1",
                      "bur_quality2"))
```

```{r}
table_summary <- stargazer(as.data.frame(vars), type = "text", out = "table_a1.html", digits = 2, title = "Summary Statistics", omit.summary.stat = c("p25", "p75"),
                    covariate.labels = c("Termination flexibility",
                      "Anytime",
                      "After initial commitment",
                      "Termination window",
                      "Effective commitment (years)",
                      
                      "Democratic accountability (Party 1)",
                      "PR protection (Party 2)",
                      
                      "Year of signature",
                      "Initial term",
                      "Automatic renewal",
                      "Unilateral termination clause",
                      "Amendment",
                      "Sunset clause",
                      "Health/environment exception",
                      "Security exception",
                      "Other exception",
                      "Prudential carveout",
                      "ISDS clause",
                      "SSDS clause",
                      "SRS substantive",
                      
                      "% FDI inflows of GDP (Party 1)",
                      "% FDI inflows of GDP (Party 2)",
                      "% Trade of GDP (Party1)",
                      "% Trade of GDP (Party 2)",
                      "Cumulative ISDS experience (Party 1)",
                      "Cumulative ISDS experience (Party 2)",
                      "Government stability (Party 1)",
                      "Government stability (Party 2)",
                      "Democracy (Party 1) (FH+Polity2, imputed NAs)",
                      "Democracy (Party 2) (FH+Polity2, imputed NAs)",
                      "Democracy (Party1) Polity2",
                      "Democracy (Party2) Polity2",
                      "Bureaucratic quality (Party 1)",
                      "Bureaucratic quality (Party 2)"))
```

Figure A1. Effective commitment period in BITs

```{r}
iias <- iias %>% mutate(term_flexibility_factor = as.factor(term_flexibility)) 
iias$term_flexibility_factor <- factor(iias$term_flexibility_factor, levels = c("Anytime", "Anytime after initial", "Termination window"))

#exclude NAs
nonas <- iias %>% filter(!is.na(term_flexibility_factor))

fig_a1 <- ggplot(data = nonas, mapping = aes(x = term_flexibility_factor, exclude = NULL, y = effective_commitment_years)) +
  geom_boxplot() +
  labs(x = "Termination flexibility",
       y = "Effective commitment period (years)") +
  ggtitle("Termination flexibility and Effective commitment period in BITs") +
  theme_clean() +
  theme(plot.title = element_text(hjust = 0.5))
fig_a1
```
